package threadTest.myAsync;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.ThreadPoolExecutor;

/**
 * 功能描述:线程池任务配置
 * 2024/09/04
 *
 * @author ZhangYi
 */
@Configuration
@EnableAsync
public class ThreadPoolTaskConfig {
	/**
	 * 核心线程数（默认线程数）
	 */
	private static final int corePoolSize = 10;
	/**
	 * 最大线程数
	 */
	private static final int maxPoolSize = 100;
	/**
	 * 允许线程空闲时间（单位：默认为秒）
	 */
	private static final int keepAliveTime = 10;
	/**
	 * 缓冲队列数
	 */
	private static final int queueCapacity = 200;
	/**
	 * 线程池名前缀
	 */
	private static final String threadNamePrefix = "CarbonlensTEC-Service-";

	@Bean("threadPoolTaskExecutor")
	public ThreadPoolTaskExecutor getAsyncExecutor() {
		ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
		executor.setCorePoolSize(corePoolSize);
		executor.setMaxPoolSize(maxPoolSize);
		executor.setQueueCapacity(queueCapacity);
		executor.setKeepAliveSeconds(keepAliveTime);
		executor.setThreadNamePrefix(threadNamePrefix);

		// 线程池对拒绝任务的处理策略
		executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
		// 初始化
		executor.initialize();
		return executor;
	}
}
